package com.example.jsondatademo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.Type;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etJson1,etJson2;
    private Button btnJson1,btnJson2,btnJson3,btnJson4;
    private TextView tvJsonText1,tvJsonText2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etJson1=(EditText) findViewById(R.id.etJson1);
        etJson2=(EditText) findViewById(R.id.etJson2);
        etJson1.setText("{name:zhangsan,age:27,married:true}");
        etJson2.setText("[{name:lisi,age:25,married:true},{name:Jason,age:20,married:false}]");
        btnJson1=(Button) findViewById(R.id.btnJson1);
        btnJson2=(Button) findViewById(R.id.btnJson2);
        btnJson3=(Button) findViewById(R.id.btnJson3);
        btnJson4=(Button) findViewById(R.id.btnJson4);
        btnJson1.setOnClickListener(this);
        btnJson2.setOnClickListener(this);
        btnJson3.setOnClickListener(this);
        btnJson4.setOnClickListener(this);
        tvJsonText1=(TextView) findViewById(R.id.tvJsonText1);
        tvJsonText2=(TextView) findViewById(R.id.tvJsonText2);
    }
    @Override
    public void onClick(View view) {
        Gson gson=new Gson();
        String name="";
        int age=0;
        boolean married=false;
        StringBuilder builder;
        switch (view.getId()){
            case R.id.btnJson1:  //使用JSONObject类
                try {
                    JSONObject jsonObject=new JSONObject(etJson1.getText().toString());
                    name=jsonObject.optString("name");
                    age=jsonObject.optInt("age");
                    married=jsonObject.optBoolean("married");
                    builder=new StringBuilder("使用JSONObject类：\n");
                    builder.append("name:"+name+",age:"+age+",married:"+married);
                    tvJsonText1.setText(builder.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btnJson2:  //使用Gson类
                Personal personal =gson.fromJson(etJson1.getText().toString(), Personal.class);
                name= personal.getName();
                age= personal.getAge();
                married= personal.getMarried();
                builder=new StringBuilder("使用Gson类：\n");
                builder.append("name:"+name+",age:"+age+",married:"+married);
                tvJsonText1.setText(builder.toString());
                break;
            case R.id.btnJson3:  //使用JSONArray类
                builder=new StringBuilder("使用JSONArray类解析：\n");
                try {
                    JSONArray jsonArray=new JSONArray(etJson2.getText().toString());
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject=jsonArray.getJSONObject(i);
                        name=jsonObject.optString("name");
                        age=jsonObject.optInt("age");
                        married=jsonObject.optBoolean("married");
                        builder.append("name:"+name+",age:"+age+",married:"+married+"\n");
                        builder.append(builder.toString());
                    }
                    tvJsonText2.setText(builder.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btnJson4: //使用Gson类
                builder=new StringBuilder("使用Gson类解析：\n");
                Type listType=new TypeToken<List<Personal>>(){}.getType();
                List<Personal> personalList =gson.fromJson(etJson2.getText().toString(),listType);
                Log.i("TAG", "onClick: "+ personalList.toString());
                for(int i = 0; i< personalList.size(); i++){
                    name= personalList.get(i).getName();
                    age= personalList.get(i).getAge();
                    married= personalList.get(i).getMarried();
                    builder.append("name:"+name+",age:"+age+",married:"+married+"\n");
                }
                tvJsonText2.setText(builder.toString());
                break;
        }
    }
}
